package com.yupi.usercenter.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.yupi.usercenter.common.BaseResponse;
import com.yupi.usercenter.common.ResultUtils;
import com.yupi.usercenter.listener.ExcelListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Api
@Slf4j
@RequestMapping("/file")
public class FileController {

    @ApiOperation(value = "读Excel")
    @PostMapping("/readExcel")
    public BaseResponse<Map<Integer,String>> readExcel(String filePath) {

        ExcelListener listener = new ExcelListener();
//        String filePath ="E:\\code\\user-center-backend\\ResourceMonitor.xlsx";
        // 这里需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
//        EasyExcel.read(filePath, Monitor.class, listener).sheet().doRead();
        Map<Integer,String> ExcelMap =new HashMap<>();
        EasyExcel.read(filePath, new PageReadListener<Map<Integer, String>>(
                dataList -> {
                    for (Map<Integer, String> data : dataList) {
                        ExcelMap.putAll(data);
                    }
                }
        )).sheet().doRead();
        return ResultUtils.success(ExcelMap);
    }

}
